package com.tca.spring.framework.learning.tx.dao;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;
import java.util.UUID;

@Component
@Slf4j
public class InfoMapper {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private Random random = new Random(100);

    @Transactional(rollbackFor = Exception.class)
    public void insert() {
        String id = UUID.randomUUID().toString().substring(0, 5);
        jdbcTemplate.update("INSERT INTO info VALUES (?, ?, ?)", random.nextInt(100), 30, id);
        int i = 10/0;
        log.info("插入成功");
    }
}